package ui.fragment.minefragment;

import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.shizhefei.mvc.ILoadViewFactory;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import base.BaseBean;
import base.fragment.BaseSwipeFragment;
import bean.OrderListBean;
import event.MainPageEvent;
import event.OrderEvent;
import student.andlei.com.student.R;
import utils.GsonUtils;
import utils.ImageLoaderUtils;
import utils.pull.OrderLoadViewFactory;
import view.WrapContentLinearLayoutManager;

/**
 * author : ChenHuiMing
 * date : 2018/9/17
 * email : 907399255@qq.com
 * description : 我的订单
 */
public class OrderListFragment extends BaseSwipeFragment<OrderListBean.ListBean> {

    public static final String GET_ORDER_LIST = "/Shop/getOrderList";
    public static final String CANCEL_ORDER = "/Shop/cancelOrder";
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;

    public static Fragment getInstance() {
        return new OrderListFragment();
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        super.success(action, baseBean);
        if (TextUtils.equals(action, CANCEL_ORDER)) {
            refresh();
        }
    }

    @Override
    protected void findViews() {
        super.findViews();
        mSwipeRefreshLayout = getView(R.id.swipeRefreshLayout);
        mRecyclerView = getView(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new RecycleViewDivider(
                getActivity(), LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.gray)));
    }

    @Override
    public SwipeRefreshLayout getSwipeRefreshLayout() {
        return mSwipeRefreshLayout;
    }

    @Override
    public String getHttpUrl() {
        return GET_ORDER_LIST;
    }

    @Override
    public Map<String, Object> getMapParam() {
        return null;
    }

    @Override
    protected void formatViews() {
        refresh();
    }

    @Override
    public ILoadViewFactory.ILoadView setLoadView() {
        OrderLoadViewFactory loadView = new OrderLoadViewFactory();
        loadView.setOnEmptyClickListener(new OrderLoadViewFactory.OnEmptyClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到商城
                MainPageEvent pageEvent = new MainPageEvent();
                pageEvent.firstPage = MainPageEvent.MALL_PAGE;
                EventBus.getDefault().post(pageEvent);
            }
        });
        return loadView;
    }

    @Override
    public SwipeData formatHttpData(boolean isRefresh, BaseBean bean) {
        OrderListBean orderListBean = (OrderListBean) GsonUtils.JSONToObject(GsonUtils.beanToJSONString(bean.data), OrderListBean.class);
        SwipeData swipeData = new SwipeData();
        swipeData.currentPage = orderListBean.page.cpage;
        swipeData.maxPage = orderListBean.page.total_page;
        swipeData.list = orderListBean.list;
        return swipeData;
    }

    @Override
    public int getItemLayoutResId() {
        return R.layout.item_order_list;
    }

    @Override
    public void viewConvert(BaseViewHolder helper, final OrderListBean.ListBean item) {
        ImageView imageView = helper.getView(R.id.iv_logo);
        ImageLoaderUtils.loadImage(getActivity(), item.schedule_image, imageView);
        helper.setText(R.id.tv_time, item.create_time).setText(R.id.tv_status, getOrderStatus(item.pay_status))
                .setText(R.id.tv_title, item.schedule_name).setText(R.id.tv_teacher, "主讲：" + item.teacher_name)
                .setText(R.id.tv_price, "￥" + item.price).setVisible(R.id.tv_pay, item.pay_status == 10)
                .setVisible(R.id.tv_pay_price, item.pay_status == 100)
                .setText(R.id.tv_pay_price, "实付：" + item.price);
        helper.setOnClickListener(R.id.tv_delete, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String, Object> map = new HashMap<>();
                map.put("order_id", item.order_id);
                post(CANCEL_ORDER, map, BaseBean.class, false);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order_list;
    }

    @Override
    public void onItemClickListener(SwipeAdapter adapter, RecyclerView.ViewHolder vh, int position) {
        super.onItemClickListener(adapter, vh, position);
        OrderListBean.ListBean listBean = adapter.getItem(position);
        OrderEvent orderEvent = new OrderEvent();
        orderEvent.order_id = listBean.order_id;
        if (listBean.pay_status == 10) {
            orderEvent.pagerIndex = 1;
            EventBus.getDefault().post(orderEvent);
        } else {
            orderEvent.pagerIndex = 2;
            EventBus.getDefault().post(orderEvent);
        }
    }


    private String getOrderStatus(int status) {
        if (status == 0) {
            return "取消";
        } else if (status == 10) {
            return "待付款";
        } else if (status == 100) {
            return "已付款";
        }
        return "";
    }
}
